<?php
//测试控制器类
class InfoController extends Controller{

    private $db;

    public function __init(){
        $user = Q('session.user');
        if(empty($user)){
            header('location:'.u('login/login'));
        }else{
            /*判断登陆信息日志ip*/
            $where = 'uid = '.$user['uid'];
            $loginfo = K('user') -> selclog($where);
            if($loginfo['logip'] != $_SESSION['logip']){
                unset($_SESSION['user']);
                $this -> success($message = '您的账户已从另一个地方登陆！',U('login/login'), $time = 2);
            }
        }
        $this -> assign('user',$user);

        //指定函数
        $this -> db = k("User");
    }
    //动作方法
    public function index(){
        //显示视图
        $this->display('Template/user/header');
        $this->display();
    }

    //修改用户资料
    public function modify(){

        $user = Q('session.user');

        $Model = K('user');

        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }
        //将获取到的数据放入数组
        $data = array(
            'nickname' => $_POST['nick'],
            'mobile' => $_POST['mobile'],
            'email' => $_POST['email'],
            'qq' => $_POST['qq'],
        );
        //条件
        $where = 'uid='.$user['uid'];
        //修改用户密码
        if($this -> db -> upinfo($where,$data)){
            $user = $this -> db -> userone('uid ="'.$user['uid'].'"');
            unset($_SESSION['user']);
            $_SESSION['user'] = $user;
            $this -> success($message = '修改成功。',U('info/index'), $time = 2);
        }else{
            $this -> error('修改失败！');
            exit;
        }

    }

    //修改用户密码界面
    public function pass(){
        $this->display('Template/user/header');
        $this->display();
    }
    //修改用户密码
    public function pwss(){
        $user = Q('session.user');
        $Model = K('user');

        //表单验证
        if(!$Model -> create()){
            $this -> error($Model -> error);
            exit;
        }

        //获取参数
        $ypw = $_POST['yuserpass'];
        $pw = $_POST['userpass'];

        if($pw == $ypw){
            $this -> error('修改失败，与原密码重复！');
            exit;
        }elseif(md5(md5($ypw)) != $user['pw']){
            $this -> error('修改失败，原始密码错误！');
            exit;
        }
        //条件
        $where = 'uid='.$user['uid'];
        $data = array('pw' => md5(md5($pw)));
        //修改用户密码
        if($this -> db -> upinfo($where,$data)){
            $user = $this -> db -> userone('uid ="'.$user['uid'].'"');
            unset($_SESSION['user']);
            $_SESSION['user'] = $user;
            $this -> success($message = '修改成功。',U('info/index'), $time = 2);
        }else{
            $this -> error('修改失败！');
            exit;
        }

    }
}
